package com.wu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.demo.common.domain.AjaxResult;
import com.wu.demo.common.domain.LoginUser;
import com.wu.demo.entity.SysLogininfor;
import com.wu.demo.entity.SysUser;
import com.wu.demo.farmework.manager.AsyncFactory;
import com.wu.demo.farmework.manager.AsyncManager;
import com.wu.demo.farmework.manager.TokenManager;
import com.wu.demo.mapper.SysUserMapper;
import com.wu.demo.service.ISysLogininforService;
import com.wu.demo.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author wu
 * @since 2020-12-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Resource
    private SysUserMapper userMapper;
    @Autowired
    private TokenManager tokenManager;
    @Resource
    private ISysLogininforService logininforService;

    private Lock lock = new ReentrantLock();
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public LoginUser login(Long userId){
        SysUser user = userMapper.selectById(userId);
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        //异步保存操作数据
        AsyncManager.me().execute(AsyncFactory.recordLoginInfo(user.getUserName(),"1","成功"));
        return tokenManager.createToken(loginUser);
    }

    @Override
    public  AjaxResult test(){
        synchronized (this){
            SysUser user = userMapper.selectById(1);
            Integer age = user.getAge();
            //TimeUnit.SECONDS.sleep(2);
            if(age <= 0){
                System.out.println(Thread.currentThread().getName() + ":失败="+age);
                return AjaxResult.error("已售完");
            }
            System.out.println(Thread.currentThread().getName() + ":原有数量="+age);
            LambdaUpdateWrapper<SysUser> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(SysUser::getAge,user.getAge() - 1).eq(SysUser::getUserId,user.getUserId());
            userMapper.update(null,updateWrapper);
            System.out.println(Thread.currentThread().getName() + "售票成功");
            return AjaxResult.success();
        }
    }

    @Override
    public  AjaxResult test2(){
        lock.lock();
            SysUser user = userMapper.selectById(1);
            Integer age = user.getAge();
            //TimeUnit.SECONDS.sleep(2);
            if(age <= 0){
                System.out.println(Thread.currentThread().getName() + ":失败="+age);
                lock.unlock();
                return AjaxResult.error("已售完");
            }
            System.out.println(Thread.currentThread().getName() + ":原有数量="+age);
            LambdaUpdateWrapper<SysUser> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(SysUser::getAge,user.getAge() - 1).eq(SysUser::getUserId,user.getUserId());
            userMapper.update(null,updateWrapper);
            System.out.println(Thread.currentThread().getName() + "售票成功");
        lock.unlock();
            return AjaxResult.success();

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUser2(SysUser user,SysLogininfor sysLogininfor){
        userMapper.insert(user);
        //
        user.setUserName("90000");
        userMapper.insert(user);
        logininforService.saveLoginInfo(sysLogininfor);
        int a= 10/0;
    }

}


